package com.huawei.hms.framework.network.restclient.hwhttp.netdiag;

import android.content.Context;
import android.os.SystemClock;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.R;
import com.huawei.hms.framework.network.util.ContextUtil;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class NetDiagManager {
    private static final int CYCLE = 3600000;
    private static final int MINUTE_TO_MILLISECOND = 60000;
    private static final String TAG = "NetDiagManager";
    private static final int TIME_OUT = 6000;
    private static int count = 0;
    private static long lastNetDiagTime;
    private static ExecutorService netDiagExecutor = Executors.newCachedThreadPool();
    private Future<QueryMetrics> tcpFuture;
    private QueryMetrics tcpMetrics;
    private Future<QueryMetrics> udpFuture;
    private QueryMetrics udpMetrics;

    public ExecutorService getNetDiagExecutor() {
        return netDiagExecutor;
    }

    public void query() {
        Context context = ContextUtil.getContext();
        if (context != null) {
            try {
                final String string = context.getString(R.string.netdiag_dnsserver_ip);
                this.tcpFuture = netDiagExecutor.submit(new Callable<QueryMetrics>() {
                    public QueryMetrics call() {
                        return new TcpConnectQuery().query(string);
                    }
                });
                this.udpFuture = netDiagExecutor.submit(new Callable<QueryMetrics>() {
                    public QueryMetrics call() {
                        return new UdpDnsQuery().query(string);
                    }
                });
            } catch (Throwable th) {
                Logger.v(TAG, "DEFAULT_DOMAIN_NAME failed: " + th);
            }
        }
    }

    public QueryMetrics getTcpMetrics() {
        return this.tcpMetrics;
    }

    public QueryMetrics getUdpMetrics() {
        return this.udpMetrics;
    }

    public void netDiag() {
        synchronized (NetDiagManager.class) {
            if (count == 0) {
                Logger.v(TAG, "netDiag count is zero");
                count++;
                lastNetDiagTime = SystemClock.elapsedRealtime();
                query();
                return;
            }
            long abs = Math.abs(SystemClock.elapsedRealtime() - lastNetDiagTime);
            if (abs > 3600000) {
                Logger.v(TAG, "netDiag interval beyond one hour");
                count = 0;
                lastNetDiagTime = SystemClock.elapsedRealtime();
                query();
                return;
            }
            long j = (long) ((((count - 1) * 10) + 5) * 60000);
            if (abs > j) {
                Logger.v(TAG, "netDiag Threshold:" + j);
                lastNetDiagTime = SystemClock.elapsedRealtime();
                count++;
                query();
            }
            Logger.v(TAG, "netDiag is prohibited ,count = " + count);
        }
    }

    public void getMetrics() {
        Object e;
        if (this.tcpFuture == null || this.udpFuture == null) {
            Logger.v(TAG, "future is null");
            return;
        }
        try {
            this.udpMetrics = (QueryMetrics) this.udpFuture.get(6000, TimeUnit.MILLISECONDS);
            this.tcpMetrics = (QueryMetrics) this.tcpFuture.get(6000, TimeUnit.MILLISECONDS);
            this.udpFuture.cancel(true);
            this.tcpFuture.cancel(true);
        } catch (InterruptedException e2) {
            e = e2;
            try {
                Logger.v(TAG, "netDiag exception:" + e.getClass().getSimpleName());
            } finally {
                this.udpFuture.cancel(true);
                this.tcpFuture.cancel(true);
            }
        } catch (TimeoutException e3) {
            e = e3;
            Logger.v(TAG, "netDiag exception:" + e.getClass().getSimpleName());
        } catch (ExecutionException e4) {
            e = e4;
            Logger.v(TAG, "netDiag exception:" + e.getClass().getSimpleName());
        }
    }

    public static void resetCount() {
        if (count > 1) {
            count = 1;
        }
    }
}
